import { ethers } from 'ethers'
import Web3 from 'web3';

// 充值-查询余额
// const { data: contractData, isError, isLoading } = useContractRead({
//   address: walletInfo && walletInfo.coinContractAddress,
//   // address: '0xdac17f958d2ee523a2206206994597c13d831ec7',
//   abi: BEP_ABI,
//   functionName: 'balanceOf',
//   args: ['0x968777a2cfb577096d0f3c8ff552b21c1b8512e4'],
//   onSuccess(data) {
//     console.log('Success', data)
//   },
//   onError(error) {
//     console.log('Error1', error)
//   },
// })

// 用原生web3获取余额

export const contactFunctionCall = (abi: any, method: any, params: any, contract: any, cb: any) => {
    const web3 = (window.ethereum && new Web3(window.ethereum)) || null;
    let iface = new ethers.utils.Interface([abi])
    let encodeData = iface.encodeFunctionData(method, params)
    web3.eth.call({ to: contract, data: encodeData }, (error: any, result: any) => {
        cb(error, iface.decodeFunctionResult(method, result))
    })
}

// 调余额和充值
// export const contactFunctionSend = (abi: any, method: any, params: any, contract: any, account: any, value: any, cb: any,) => {
//     const web3 = (window.ethereum && new Web3(window.ethereum)) || null;
//     let iface = new ethers.utils.Interface([abi])
//     let encodeData = iface.encodeFunctionData(method, params)
//     console.log("encodeData-" + method, encodeData)

//     let tx = {
//         from: account,
//         to: contract,
//         value: value,
//         data: encodeData
//     }
//     web3.eth.estimateGas(tx, (gasError: any, gasResult: any) => {
//         if (gasError) {
//             let err = JSON.parse(gasError.message.substr(gasError.message.indexOf("{")))
//             // alert(err.message.substr(err.message.indexOf(":") + 2))
//             cb(err, null)
//             return
//         }
//         //方法2，sync
//         web3.eth.sendTransaction(tx)
//             .then((receipt: any) => {
//                 console.log(method, receipt)
//                 cb({}, receipt)
//             }).catch(() => {
//                 // console.log('拒绝了授权')

//             })

//     });
// }